package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.context.BaseContext;
import com.ydd.dto.*;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddUser;
import com.ydd.entity.YddUserData;
import com.ydd.exception.BaseException;
import com.ydd.mapper.ArticleMapper;
import com.ydd.mapper.UserArticleMapper;
import com.ydd.mapper.UserMapper;
import com.ydd.service.UserArticleService;
import com.ydd.vo.DataListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserArticleServiceImpl implements UserArticleService {
    @Autowired
    private UserArticleMapper userArticleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 用户中心
     *
     * @param
     * @return
     */
    @Override
    public UserInfoDTO info() {
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        Integer userCurrentId = BaseContext.getUserCurrentId();
        YddUser yddUser = userMapper.selectById(userCurrentId);
        userInfoDTO.setUserInfo(yddUser);
        Integer ArticleNumTotal = userArticleMapper.selectAll(userCurrentId);
        userInfoDTO.setArticleNum(ArticleNumTotal.toString());

        Integer zanNum = userArticleMapper.selectZan(userCurrentId);
        userInfoDTO.setZanNum(zanNum.toString());
        Integer collectNum = userArticleMapper.selectCollect(userCurrentId);
        userInfoDTO.setCollectNum(collectNum.toString());

        return userInfoDTO;
    }

    /**
     * 我的文章
     *
     * @return
     * @parame userArticleRequestDTO
     */

    @Override
    public UserArticleDTO MyArticle(UserArticleRequestDTO userArticleRequestDTO) {
        PageHelper.startPage(Integer.parseInt(userArticleRequestDTO.getPage()), Integer.parseInt(userArticleRequestDTO.getLimit()));
        Integer userCurrentId = BaseContext.getUserCurrentId();
        List<YddArticle> yddArticle = userArticleMapper.selectOrder(userCurrentId);
        String author = yddArticle.get(0).getAuthor();
        Page<YddArticle> page = userArticleMapper.pageQuery(userArticleRequestDTO, author);
        long total = page.getTotal();
        List<YddArticle> records = page.getResult();
        UserArticleDTO userArticleDTO = new UserArticleDTO();
        userArticleDTO.setCount(new Long(total).intValue());
        userArticleDTO.setPageSize(Integer.parseInt(userArticleRequestDTO.getLimit()));
        userArticleDTO.setTotalPages(page.getPages());
        userArticleDTO.setCurrentPage(Integer.parseInt(userArticleRequestDTO.getPage()));
        userArticleDTO.setData(yddArticle);
        return userArticleDTO;

    }

    /**
     * 收藏与点赞
     *
     * @param dataListDTO
     * @return
     */
    @Override
    public DataListVO userDataList(DataListDTO dataListDTO) {
        PageHelper.startPage(Integer.parseInt(dataListDTO.getPage()), Integer.parseInt(dataListDTO.getLimit()));
        if ("2".equals(dataListDTO.getType())) {
            Integer userCurrentId = BaseContext.getUserCurrentId();
            Page<YddArticle> myZan = userArticleMapper.myZan(dataListDTO, userCurrentId);
            long total = myZan.getTotal();
            List<YddArticle> records = myZan.getResult();
            DataListVO dataListVO = new DataListVO();
            dataListVO.setCount(new Long(total).intValue());
            dataListVO.setPageSize(Integer.parseInt(dataListDTO.getLimit()));
            dataListVO.setTotalPages(myZan.getPages());
            dataListVO.setCurrentPage(Integer.parseInt(dataListDTO.getPage()));
            dataListVO.setData(records.toArray());
            return dataListVO;
        } else {
            Integer userCurrentId = BaseContext.getUserCurrentId();
            Page<YddArticle> myCollect = userArticleMapper.myCollect(dataListDTO, userCurrentId);
            long total = myCollect.getTotal();
            List<YddArticle> records = myCollect.getResult();
            DataListVO dataListVO = new DataListVO();
            dataListVO.setCount(new Long(total).intValue());
            dataListVO.setPageSize(Integer.parseInt(dataListDTO.getLimit()));
            dataListVO.setTotalPages(myCollect.getPages());
            dataListVO.setCurrentPage(Integer.parseInt(dataListDTO.getPage()));
            dataListVO.setData(records.toArray());
            return dataListVO;
        }
    }

    /**
     * 取消收藏
     *
     * @param cancelListDTO
     */
    @Override
    public void cancel(CancelListDTO cancelListDTO) {
        //1为收藏，2为点赞
        //del:取消  add:增加
        Integer userCurrentId = BaseContext.getUserCurrentId();
        //判断收到的请求为取消还是增加
        if (cancelListDTO.getAction().equals("del")) {

            YddArticle yddArticle = articleMapper.selectById(cancelListDTO.getArticle_id());

//            LambdaQueryWrapper<YddUserData> eq = new LambdaQueryWrapper<YddUserData>().eq(YddUserData::getUserId, userCurrentId)
//                    .eq(YddUserData::getArticleId, cancelListDTO.getArticle_id())
//                    .eq(YddUserData::getType, 2);

            if (cancelListDTO.getType().equals("2")) {
                yddArticle.setLovenum(String.valueOf(Integer.parseInt(yddArticle.getLovenum()) - 1));
            }

            userArticleMapper.cancelAction(yddArticle);
            userMapper.delByArticleId(yddArticle.getId(),cancelListDTO.getType());
        } else {
            YddArticle yddArticle = articleMapper.selectById(cancelListDTO.getArticle_id());
            //ydd_user_date 中间表 添加点赞或收藏数据
            YddUserData yddUserData = new YddUserData();
            yddUserData.setUserId(userCurrentId);
            yddUserData.setArticleId(yddArticle.getId());
            yddUserData.setType(Integer.parseInt(cancelListDTO.getType()));
            //            YddUserData isYddUserData = userArticleMapper.selectByDate(yddUserData);

//            if (isYddUserData == null) {
            userArticleMapper.insert(yddUserData); // 插入中间表
            // 查找出对应文章 并添加喜爱数
            if (cancelListDTO.getType().equals("2")) {
                yddArticle.setLovenum(String.valueOf(Integer.parseInt(yddArticle.getLovenum()) + 1));
                articleMapper.updateById(yddArticle);// 增加点赞数
            }
//            } else {
//                // 插入中间表记录
//                throw new BaseException("已点攒或收藏");
//            }
//            }
        }
    }


    /**
     * 用户信息修改
     *
     * @param yddUser
     * @return
     */
    @Override
    public void update(YddUser yddUser) {
        YddUser yddUsers = new YddUser();
        BeanUtils.copyProperties(yddUser, yddUsers);
        userArticleMapper.update(yddUsers);
    }

    /**
     * 上传文章
     *
     * @param newArticleDTO
     */
    @Override
    public void insert(NewArticleDTO newArticleDTO) {

        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(newArticleDTO, yddArticle);
        Integer userId = BaseContext.getUserCurrentId();
        yddArticle.setUserId(userId);
        yddArticle.setDefaultData("1");
        yddArticle.setStatus(Integer.parseInt(newArticleDTO.getStatus()));
        yddArticle.setCreateDate(System.currentTimeMillis());
        yddArticle.setUpdateDate(System.currentTimeMillis());
        yddArticle.setClick(0L);
        yddArticle.setLovenum("0");
        yddArticle.setIshot(0);
        yddArticle.setIstop(0);
        //String authName = userArticleMapper.selectAuthor(userId);
        //yddArticle.setAuthor(authName);
        //Integer status = userArticleMapper.selectStatus(userId);
        //yddArticle.setStatus(status);
        userArticleMapper.InsertOne(yddArticle);

    }


    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @Override
    public void del(Long id) {
        userArticleMapper.del(id);
    }

    /**
     * 修改文章
     *
     * @param draftDTO
     */
    @Override
    public void edit(DraftDTO draftDTO) {
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(draftDTO, yddArticle);
        yddArticle.setId(Integer.parseInt(draftDTO.getId()));
        userArticleMapper.edit(yddArticle);


    }


}
